home *** CD-ROM | disk | FTP | other *** search
- package sun.tools.zip;
-
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.util.Enumeration;
- import java.util.Hashtable;
-
- public class ZipFile implements ZipConstants {
- private RandomAccessFile file;
- private String path;
- private Hashtable table;
- long cenpos;
- private long endpos;
- private long pos;
- private static final int INBUFSIZ = 64;
-
- public ZipFile(String var1) throws ZipFormatException, IOException {
- this.file = new RandomAccessFile(var1, "r");
- this.path = var1;
- this.init();
- }
-
- public ZipFile(File var1) throws ZipFormatException, IOException {
- this(var1.getPath());
- }
-
- public ZipEntry getEntry(String var1) {
- return (ZipEntry)this.table.get(var1);
- }
-
- public InputStream getInputStream(ZipEntry var1) throws ZipFormatException, IOException {
- return new ZipFileInputStream(this, var1);
- }
-
- public String getPath() {
- return this.path;
- }
-
- public Enumeration entries() {
- return this.table.elements();
- }
-
- public void close() throws IOException {
- if (this.file != null) {
- this.file.close();
- this.file = null;
- }
-
- }
-
- synchronized int read(long var1, byte[] var3, int var4, int var5) throws IOException {
- if (var1 != this.pos) {
- this.file.seek(var1);
- }
-
- int var6 = this.file.read(var3, var4, var5);
- if (var6 > 0) {
- this.pos = var1 + (long)var6;
- }
-
- return var6;
- }
-
- synchronized int read() throws IOException {
- if (this.pos != this.pos) {
- this.file.seek(this.pos);
- }
-
- int var1 = this.file.read();
- if (var1 > 0) {
- ++this.pos;
- }
-
- return var1;
- }
-
- private void init() throws ZipFormatException, IOException {
- if (!this.findEnd()) {
- throw new ZipFormatException("Could not find END header");
- } else {
- byte[] var1 = new byte[22];
- RandomAccessFile var2 = this.file;
- var2.readFully(var1, 0, var1.length);
- byte[] var11 = ZipConstants.ENDSIG;
- if (!checkSig(var1, 0, var11)) {
- throw new ZipFormatException("Invalid END header signature");
- } else {
- this.cenpos = (long)(var1[16] & 255 | (var1[17] & 255) << 8 | (var1[18] & 255 | (var1[19] & 255) << 8) << 16);
- int var12 = (int)((long)(var1[12] & 255 | (var1[13] & 255) << 8 | (var1[14] & 255 | (var1[15] & 255) << 8) << 16));
- if (this.cenpos + (long)var12 != this.endpos) {
- throw new ZipFormatException("Invalid END header format");
- } else {
- int var3 = var1[10] & 255 | (var1[11] & 255) << 8;
- if (var3 * 46 > var12) {
- throw new ZipFormatException("Invalid END header format");
- } else if ((var1[8] & 255 | (var1[9] & 255) << 8) != var3) {
- throw new ZipFormatException("Contains more than one drive");
- } else {
- this.file.seek(this.cenpos);
- byte[] var4 = new byte[var12];
- RandomAccessFile var5 = this.file;
- var5.readFully(var4, 0, var4.length);
- this.table = new Hashtable(var3);
-
- int var19;
- int var27;
- for(int var13 = 0; var13 < var12; var13 += var27 + (var4[var19] & 255 | (var4[var19 + 1] & 255) << 8)) {
- if (!checkSig(var4, var13, ZipConstants.CENSIG)) {
- throw new ZipFormatException("Invalid CEN header signature");
- }
-
- int var6 = var13 + 28;
- var6 = var4[var6] & 255 | (var4[var6 + 1] & 255) << 8;
- if (var6 == 0 || var13 + 46 + var6 > var12) {
- throw new ZipFormatException("Invalid CEN header format");
- }
-
- String var7 = new String(var4, 0, var13 + 46, var6);
- ZipEntry var8 = new ZipEntry(var7);
- var19 = var13 + 42;
- var27 = var4[var19] & 255 | (var4[var19 + 1] & 255) << 8;
- int var10 = var19 + 2;
- var8.locpos = (long)(var27 | (var4[var10] & 255 | (var4[var10 + 1] & 255) << 8) << 16);
- var19 = var13 + 20;
- var27 = var4[var19] & 255 | (var4[var19 + 1] & 255) << 8;
- var10 = var19 + 2;
- if (var8.locpos + (long)(var27 | (var4[var10] & 255 | (var4[var10 + 1] & 255) << 8) << 16) > this.cenpos) {
- throw new ZipFormatException("Invalid CEN header format");
- }
-
- var19 = var13 + 24;
- var27 = var4[var19] & 255 | (var4[var19 + 1] & 255) << 8;
- var10 = var19 + 2;
- var8.length = (long)(var27 | (var4[var10] & 255 | (var4[var10 + 1] & 255) << 8) << 16);
- var19 = var13 + 12;
- var27 = var4[var19] & 255 | (var4[var19 + 1] & 255) << 8;
- var10 = var19 + 2;
- var8.mtime = (long)(var27 | (var4[var10] & 255 | (var4[var10 + 1] & 255) << 8) << 16);
- if (this.table.put(var8.getPath(), var8) != null) {
- throw new ZipFormatException("Invalid CEN header format");
- }
-
- var27 = 46 + var6;
- var19 = var13 + 30;
- var27 += var4[var19] & 255 | (var4[var19 + 1] & 255) << 8;
- var19 = var13 + 32;
- }
-
- if (this.table.size() != var3) {
- throw new ZipFormatException("Invalid CEN header format");
- }
- }
- }
- }
- }
- }
-
- private boolean findEnd() throws IOException {
- long var1 = this.file.length();
- this.file.seek(var1);
- long var3 = var1 - 65535L;
- var3 = var3 <= 0L ? 0L : var3;
- byte[] var5 = new byte[68];
- int var6 = 64;
-
- for(int var7 = 4; var7-- > 0; var5[var6++] = 0) {
- }
-
- this.pos = var1;
-
- while(this.pos > var3) {
- var6 = (int)(this.pos - var3);
- var6 = var6 <= 64 ? var6 : 64;
- this.pos -= (long)var6;
- this.file.seek(this.pos);
- this.file.readFully(var5, 0, var6);
-
- while(true) {
- --var6;
- if (var6 <= 0) {
- break;
- }
-
- if (checkSig(var5, var6, ZipConstants.ENDSIG)) {
- this.endpos = this.pos + (long)var6;
- if (var1 - this.endpos >= 22L) {
- this.file.seek(this.endpos);
- byte[] var12 = new byte[22];
- RandomAccessFile var8 = this.file;
- var8.readFully(var12, 0, var12.length);
- int var13 = var12[20] & 255 | (var12[21] & 255) << 8;
- if (this.endpos + 22L + (long)var13 == var1) {
- this.file.seek(this.endpos);
- this.pos = this.endpos;
- return true;
- }
- }
- }
- }
- }
-
- return false;
- }
-
- static final boolean checkSig(byte[] var0, int var1, byte[] var2) {
- for(int var3 = 0; var3 < 4; ++var3) {
- if (var0[var1 + var3] != var2[var3]) {
- return false;
- }
- }
-
- return true;
- }
-
- static final boolean checkSig(byte[] var0, byte[] var1) {
- return checkSig(var0, 0, var1);
- }
-
- static final void setBytes(byte[] var0, int var1, int var2, int var3) {
- while(var2-- > 0) {
- var0[var1++] = (byte)var3;
- }
-
- }
-
- static final int getWord(byte[] var0, int var1) {
- return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
- }
-
- static final long getLong(byte[] var0, int var1) {
- int var10000 = var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
- int var2 = var1 + 2;
- return (long)(var10000 | (var0[var2] & 255 | (var0[var2 + 1] & 255) << 8) << 16);
- }
- }
-